<?php

class AppsCommentsController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view' , 'delete'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model = $this->loadModel($id) ;
		$this->setData() ;
		$model->app_id = $this::$apps[$model->app_id] ;
		$model->user_id = $this::$users[$model->user_id] ;
		$model->status = $this::$cnstatus[$model->status] ;
		$this->render('view',array(
			'model'=>$model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id) ;
		$model->status = 'removed' ;
		$model->save();
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model=new AppsComments('search');
		$model->unsetAttributes();  // clear any default values
		
		if(isset($_GET['app_id']))
			$model->app_id = $_GET['app_id'];
		if(isset($_GET['AppsComments']))
		{
			$model->attributes=$_GET['AppsComments'];
		}

		$this->render('admin',array(
			'model'=>$model,
			'cnstatus' => $this::$cnstatus,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=AppsComments::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='apps-comments-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	private static $apps = array() ;
	private static $users = array() ;
	private function setApps()
	{
		$appss = Apps::model()->findAll() ;
		foreach ($appss as $app )
		{
			$this::$apps[$app->app_id] = $app->app_name ;
		}
	}
	private function setUsers()
	{
		$us = Users::model()->findAll() ;
		foreach ($us as $user)
		{
			$this::$users[$user->user_id] = $user->login_name ;
		}
	}
	private function setData()
	{
		$this->setApps() ;
		$this->setUsers() ;
	}
	
	public function showApps($data, $row, $c)
	{
		$this->setApps() ;
		return $this::$apps[$data->app_id] ;
	}
	public function showUsers($data, $row, $c)
	{
		$this->setUsers() ;
		return $this::$users[$data->user_id] ;
	}
	
	private static $cnstatus = array('ready'=>'准备', 'locked'=>'锁定', 'open'=>'打开', 'removed'=>'删除');
	public function showStatus($data, $row, $c)
	{
		return $this::$cnstatus[$data->status] ;
	}
	
	
	
	
	
	
	
	
}
